local xushen = fk.CreateSkill {
  name = "mini__xushen",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["mini__xushen"] = "许身",
  [":mini__xushen"] = "限定技，当你脱离濒死状态后，你可以回复1点体力，然后若关索不在场，你可以令一名"..
  "其他角色选择是否用关索代替其武将并令其摸三张牌。",

  ["#mini__xushen-choose"] = "许身：你可以令一名角色选择是否变身为小程序关索并摸三张牌！",
  ["#mini__xushen-invoke"] = "许身：你可以变身为小程序关索并摸三张牌！",

  ["$mini__xushen1"] = "投我以木桃，报君以琼瑶。",
  ["$mini__xushen2"] = "君若含情，妾定有意……",
}

xushen:addEffect(fk.AfterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xushen.name) and
      player:isWounded() and player:usedSkillTimes(xushen.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover{
      who = player,
      num = 1,
      recoverBy = player,
      skillName = xushen.name,
    }
    if player.dead then return end
    if #room:getOtherPlayers(player, false) > 0 and
      not table.find(room.alive_players, function(p)
        return string.find(p.general, "guansuo") ~= nil or string.find(p.deputyGeneral, "guansuo") ~= nil
      end) then
      local to = room:askToChoosePlayers(player, {
        targets = room:getOtherPlayers(player, false),
        min_num = 1,
        max_num = 1,
        prompt = "#mini__xushen-choose",
        skill_name = xushen.name,
      })
      if #to > 0 then
        to = to[1]
        if room:askToSkillInvoke(to, {
          skill_name = xushen.name,
          prompt = "#mini__xushen-invoke",
        }) then
          room:changeHero(to, "mini__guansuo", false, false, true, true, true)
          if not to.dead then
            to:drawCards(3, xushen.name)
          end
        end
      end
    end
  end,
})

return xushen
